#!/bin/bash
# The MIT License (MIT)
# Copyright (C) 2024 Huawei Device Co., Ltd.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd)
ARKTS_NAPI_SDV_DIR=$SCRIPT_DIR/../arkts_napi_sdv
ARKTS_RS_ETS_DIR=$SCRIPT_DIR/../arkts_rs_ets
TARGET_DIR=$SCRIPT_DIR/../../target
SDK_PATH=$SCRIPT_DIR/prebuilts/ohos-sdk/linux
OUTPUT_DIR=$SCRIPT_DIR/../../output

function build_rust_so_x86_64() {
    chmod 777 $SCRIPT_DIR/x86_64-unknown-linux-ohos-clang.sh
    cd $ARKTS_NAPI_SDV_DIR
    cargo build --target x86_64-unknown-linux-ohos --release
    if [ $? -ne 0 ]; then
        echo "Compilation x86_64-unknown-linux-ohos failed"
        exit -1
    fi
    echo "Compilation x86_64-unknown-linux-ohos succeeded"
    cp $TARGET_DIR/x86_64-unknown-linux-ohos/release/libarkts_napi_sdv.so $ARKTS_RS_ETS_DIR/entry/libs/x86_64 -f
}

function build_rust_so_armv7() {
    chmod 777 $SCRIPT_DIR/armv7-unknown-linux-ohos-clang.sh
    cd $ARKTS_NAPI_SDV_DIR
    cargo build --target armv7-unknown-linux-ohos --release
    if [ $? -ne 0 ]; then
        echo "Compilation armv7-unknown-linux-ohos failed"
        exit -1
    fi
    echo "Compilation armv7-unknown-linux-ohos succeeded"
    cp $TARGET_DIR/armv7-unknown-linux-ohos/release/libarkts_napi_sdv.so $ARKTS_RS_ETS_DIR/entry/libs/armeabi-v7a -f
}

function build_rust_so_arm64() {
    chmod 777 $SCRIPT_DIR/aarch64-unknown-linux-ohos-clang.sh
    cd $ARKTS_NAPI_SDV_DIR
    cargo build --target aarch64-unknown-linux-ohos --release
    if [ $? -ne 0 ]; then
        echo "Compilation aarch64-unknown-linux-ohos failed"
        exit -1
    fi
    echo "Compilation aarch64-unknown-linux-ohos succeeded"
    cp $TARGET_DIR/aarch64-unknown-linux-ohos/release/libarkts_napi_sdv.so $ARKTS_RS_ETS_DIR/entry/libs/arm64-v8a -f
}

function download_rust() {
    if command -v rustc >/dev/null 2>&1; then
        echo "Rust is installed."
    else
        echo "Rust is not installed, installing now..."
        export RUSTUP_DIST_SERVER="https://rsproxy.cn"
        export RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup"
        curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
        source /$HOME/.cargo/env
        rustc --version
        rustup target add x86_64-unknown-linux-ohos
        rustup target add armv7-unknown-linux-ohos
        rustup target add aarch64-unknown-linux-ohos
    fi
}

function download_sdk() {
    echo "[ARKTS_RS INFO] Current python version is $(python --version)"
    if [ ! -d $SDK_PATH/12 ]; then
      python3 $SCRIPT_DIR/download_sdk.py --branch master --product-name ohos-sdk-full-api --api-version 12
    fi

    export OHOS_SDK_PATH=$SDK_PATH/12
}

function download_oh_commandline_tools() {
    if [ ! -d $SCRIPT_DIR/prebuilts ]; then
        mkdir $SCRIPT_DIR/prebuilts
    fi
    cd $SCRIPT_DIR/prebuilts

    if [ ! -f "./oh-command-line-tools/ohpm/bin/ohpm" ]; then
      echo "[ARKTS_RS INFO] download oh-command-line-tools"
      wget https://repo.huaweicloud.com/harmonyos/ohpm/5.0.2/oh-command-line-tools-20240715.zip -O ohcommandline-tools-linux.zip
      unzip ohcommandline-tools-linux.zip > /dev/null 2>&1
    fi

    OHPM_HOME=$SCRIPT_DIR/prebuilts/oh-command-line-tools/ohpm
    chmod +x $OHPM_HOME/bin/*
    $OHPM_HOME/bin/init > /dev/null 2>&1
    export PATH=$OHPM_HOME/bin:$PATH
    echo "[ARKTS_RS INFO] Current ohpm version is $(ohpm -v)"

    ohpm config set registry https://ohpm.openharmony.cn/ohpm/
    ohpm config set strict_ssl false
}

function download_nodejs() {
    if [ ! -d $SCRIPT_DIR/prebuilts ]; then
        mkdir $SCRIPT_DIR/prebuilts
    fi
    cd $SCRIPT_DIR/prebuilts

    if [ ! -f "./node-v16.20.2-linux-x64/bin/node" ]; then
        echo "[ARKTS_RS INFO] download node-v16.20.2-linux-x64.tar.gz"
        wget https://mirrors.huaweicloud.com/nodejs/v16.20.2/node-v16.20.2-linux-x64.tar.gz -O nodejs-linux-x64.tar.gz
        tar -vxzf ./nodejs-linux-x64.tar.gz > /dev/null 2>&1
    fi

    NODEJS_HOME=$SCRIPT_DIR/prebuilts/node-v16.20.2-linux-x64/
    chmod +x $NODEJS_HOME/bin/*
    export PATH=$NODEJS_HOME/bin:$PATH
    export NODE_HOME=$NODEJS_HOME
    echo "[ARKTS_RS INFO] Current node version is $(node -v)"
    echo "[ARKTS_RS INFO] Current npm version is $(npm -v)"
    npm config set registry https://repo.huaweicloud.com/repository/npm/
    npm config set @ohos:registry https://repo.harmonyos.com/npm/
    npm config set strict-ssl false
}

function build_hap() {
    export OHOS_BASE_SDK_HOME=$SDK_PATH

    cd $ARKTS_RS_ETS_DIR
    ohpm install
    chmod +x ./hvigorw
    ./hvigorw -v
    echo "[ARKTS_RS INFO] Current hvigorw version is $(./hvigorw -v)"
    ./hvigorw clean --no-daemon
    ./hvigorw --mode module -p module=entry@ohosTest -p isOhosTest=true -p buildMode=test assembleHap --analyze=normal --no-daemon
    if [ $? -ne 0 ]; then
        echo "Compilation ArkTS hap failed"
        exit -1
    fi
    echo "Compilation ArkTS hap succeeded"
}

function sign_hap() {
    if [ ! -d $SCRIPT_DIR/prebuilts ]; then
        mkdir $SCRIPT_DIR/prebuilts
    fi
    cd $SCRIPT_DIR/prebuilts

    if [ ! -d $SCRIPT_DIR/prebuilts/developtools_hapsigner/dist ]; then
      git clone https://gitee.com/openharmony/developtools_hapsigner.git
    fi

    arg_apl="normal"
    arg_feature="hos_normal_app"
    arg_profile="UnsgnedReleasedProfileTemplate.json"
    bundle_name="com.example.arkts_rs_ets"
    nosign_hap_file=$ARKTS_RS_ETS_DIR/entry/build/default/outputs/ohosTest/entry-ohosTest-unsigned.hap
    sign_hap_file=$ARKTS_RS_ETS_DIR/entry/build/default/outputs/ohosTest/entry-ohosTest-signed.hap

    cd $SCRIPT_DIR/prebuilts/developtools_hapsigner/dist
    sed -i "s/\"com.OpenHarmony.app.test\"/\"${bundle_name}\"/g" ${arg_profile}
    sed -i "s/\"normal\"/\"${arg_apl}\"/g" ${arg_profile}
    sed -i "s/\"system_basic\"/\"${arg_apl}\"/g" ${arg_profile}
    sed -i "s/\"system_core\"/\"${arg_apl}\"/g" ${arg_profile}
    sed -i "s/\"hos_normal_app\"/\"${arg_feature}\"/g" ${arg_profile}
    sed -i "s/\"hos_system_app\"/\"${arg_feature}\"/g" ${arg_profile}
    java -jar hap-sign-tool.jar  sign-profile -keyAlias "openharmony application profile release" -signAlg "SHA256withECDSA" -mode "localSign" -profileCertFile "OpenHarmonyProfileRelease.pem" -inFile "${arg_profile}" -keystoreFile "OpenHarmony.p12" -outFile "ArktsNapiRs.p7b" -keyPwd "123456" -keystorePwd "123456"
    java -jar hap-sign-tool.jar sign-app -keyAlias "openharmony application release" -signAlg "SHA256withECDSA" -mode "localSign" -appCertFile "OpenHarmonyApplication.pem" -profileFile "ArktsNapiRs.p7b" -inFile "${nosign_hap_file}" -keystoreFile "OpenHarmony.p12" -outFile "${sign_hap_file}" -keyPwd "123456" -keystorePwd "123456"
}

function output() {
    if [ -d "$OUTPUT_DIR" ]; then
        rm -rf $OUTPUT_DIR
    fi
    mkdir $OUTPUT_DIR
    cp $ARKTS_RS_ETS_DIR $OUTPUT_DIR -rf
    mkdir $OUTPUT_DIR/testcases
    cp $ARKTS_RS_ETS_DIR/entry/build/default/outputs/ohosTest/* $OUTPUT_DIR/testcases -f
    cp $SCRIPT_DIR/run.bat $OUTPUT_DIR -f
}

echo "pwd = $(pwd)"

download_rust

download_sdk

build_rust_so_x86_64

build_rust_so_armv7

build_rust_so_arm64

download_nodejs

download_oh_commandline_tools

build_hap

sign_hap

output
